package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.dao.ITimePeriodWeekDao;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.TimePeriodWeek;
import com.magicode.cms.service.IChannelService;
import com.magicode.cms.service.ITimePeriodWeekService;

@Service("serviceTimeWeek")
public class TimePeriodWeekServiceImpl extends BaseServiceImpl<TimePeriodWeek,ITimePeriodWeekDao> implements ITimePeriodWeekService,UtilPath{

	@Autowired
	@Qualifier("daoTimeweek")
	private ITimePeriodWeekDao weekDao;

	public ITimePeriodWeekDao getWeekDao() {
		return weekDao;
	}

	public void setWeekDao(ITimePeriodWeekDao weekDao) {
		this.weekDao = weekDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "yeartitle");
		map.put(json_title, "年度");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "beginDate");
		map.put(json_title, "开始时间");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "endDate");
		map.put(json_title, "结束时间");
		map.put("width", 200);
		beaninfo.add(map);
		
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.TimePeriodWeek.class.getName()+" t"
				+ " left join t.year as year "
				+ " where 1 = 1 ";
		if(null!=querywhere && !querywhere.isEmpty()){
			
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
			}

			if(querywhere.containsKey("yearTitle")){
				hql = hql+" and "+entity_alias_name+".year.title = '"+querywhere.get("yearTitle")+"'";
			}

			if(querywhere.containsKey("weekTitle")){
				hql = hql+" and "+entity_alias_name+".title = '"+querywhere.get("weekTitle")+"'";
			}
			/*	if(map.containsKey("parentId")){
				hql = hql+" and "+entity_alias_name+".parent.id='"+map.get("parentId")+"'";
			}

			if(map.containsKey("type")){
				hql = hql+" and "+entity_alias_name+".type in ("+map.get("type")+")";
			}
			if(map.containsKey("nocode_quicklytReport")){
				String pram1 = map.get("nocode_quicklytReport");
				hql = hql+" and "+entity_alias_name+".code not in ('"+pram1+"')";
			}*/
			System.out.println(hql);
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere,0, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("yeartitle"," year.title as yeartitle ");
			gridsel.put("beginDate", ""+entity_alias_name+".beginDate as beginDate ");
			gridsel.put("endDate", ""+entity_alias_name+".endDate as endDate ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public ITimePeriodWeekDao getDao() throws Exception {
		return weekDao;
	}

	@Override
	public TimePeriodWeek getWeek(String year, String week) throws Exception {
		Map<String, String> map=new HashMap<>();
		map.put("yearTitle", year);
		map.put("weekTitle", week);
		String hql=this.getHql(map);
		List<TimePeriodWeek> list=queryDataList("select t "+hql, 0, 0);
		return list.size()>0?list.get(0):null;
	}

	@Override
	public List<Map<String, Object>> simpleSelect(Map<String, String> querywhere) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Object> getOriginList(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> handleOriginList(
			List<Object> yearWeekOriginList) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
